const express = require("express");
const pool = require("../pool.js");
//使用理由器
var router = express.Router();
//增加
router.post("/add", (req, res) => {
	var obj = req.body;
	var sql = "insert into product_class set ?"
	pool.query(sql, obj, (err, result) => {
		if(err) throw err;
		if(result.affectedRows > 0) {
			res.send({
				"code": 200,
				"msg": "添加成功"
			});
		} else {
			res.send({
				"code": 301,
				"msg": "添加失败"
			});
		}
	})
})
//获取列表
router.get("/list", (req, res) => {
	var obj = req.query;
	var _fid = obj.fid;
	var _page = obj.page;
	var _limit = obj.limit;
	if(!_page) {
		_page = 1;
	}
	if(!_limit) {
		_limit = 10;
	}
	//计算从第几条数据开始
	_page = (_page - 1) * _limit;
	//把字符串转化为整数
	_page = parseInt(_page);
	_limit = parseInt(_limit);
	if(_fid != undefined) {
		var sql = "SELECT * FROM product_class WHERE fid=?";
		pool.query(sql, _fid, (err, result) => {
			if(err) throw err;
			res.send(result);
		});
	} else {
		var sql = "SELECT * FROM product_class LIMIT ?,?";
		pool.query(sql, [_page, _limit], (err, result) => {
			if(err) throw err;
			pool.query("SELECT COUNT(*) AS count FROM product_class;", (error, resu) => {
				if(error) throw error;
				resu = JSON.stringify(resu[0]);
				resu = JSON.parse(resu);
				var count = resu.count;
				res.send({
					"code": 0,
					"count": count,
					"data": result
				})
			})
		})
	}
});
//删除
router.get("/delete", (req, res) => {
	var _class_id = req.query.class_id;
	pool.query("SELECT * FROM product WHERE class_id=?", _class_id, (error, resu) => {
		if(error) throw error;
		if(resu.length > 0) {
			res.send({
				"code": 400,
				"msg": "该分类下还有商品"
			});
		} else {
			pool.query("DELETE FROM product_class WHERE class_id=?", _class_id, (err, result) => {
				if(err) throw err;
				if(result.affectedRows > 0) {
					res.send({
						"code": 200,
						"msg": "删除成功"
					});
				} else {
					res.send({
						"code": 301,
						"msg": "删除失败"
					});
				}
			})
		}
	})

})

router.get("/del_all", (req, res)=>{
	var _class_id = req.query.class_id;
	pool.query("DELETE FROM product WHERE class_id=?",_class_id,(err,result)=>{
		if (err) throw err;
		pool.query("DELETE FROM product_class WHERE class_id=?", _class_id, (error, resu) => {
				if(error) throw resu;
				if(result.affectedRows > 0) {
					res.send({
						"code": 200,
						"msg": "删除成功"
					});
				} else {
					res.send({
						"code": 301,
						"msg": "删除失败"
					});
				}
			})
	})
})


//获取详情
router.get("/details", (req, res) => {
	var _class_id = req.query.class_id;
	var sql = "SELECT * FROM product_class WHERE class_id=?";
	pool.query(sql, _class_id, (err, result) => {
		if(err) throw err;
		if(result.length) {
			res.send(result);
		} else {
			res.send({
				"code": 301,
				"msg": "查询失败"
			})
		}
	})
})
//编辑
router.post("/update", (req, res) => {
	var obj = req.body;
	for(var key in obj) {
		if(!obj[key]) {
			res.send({
				"code": 301,
				"msg": key + "不能为空"
			})
			return;
		}
	}
	var _class_id = obj.class_id;
	delete obj.class_id;
	var sql = "UPDATE product_class SET ? WHERE class_id=?";
	pool.query(sql, [obj, _class_id], (err, result) => {
		if(err) throw err;
		if(result.affectedRows > 0) {
			res.send({
				"code": 200,
				"msg": "修改成功"
			})
		} else {
			res.send({
				"code": 301,
				"msg": "修改失败"
			})
		}
	})
})
//导出路由器
module.exports = router;